package com.byxm.wlgw.control.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.byxm.wlgw.bean.JgqjBean;
import com.byxm.wlgw.bean.Splb_fBean;
import com.byxm.wlgw.bean.Splb_sBean;
import com.byxm.wlgw.bean.SpxhBean;
import com.byxm.wlgw.bean.SpppBean;
import com.byxm.wlgw.bean.SpxxBean;
import com.byxm.wlgw.bean.SpxxpxBean;
import com.byxm.wlgw.common.Pages;
import com.byxm.wlgw.control.BaseAction;
import com.byxm.wlgw.service.JgqjService;
import com.byxm.wlgw.service.Splb_fService;
import com.byxm.wlgw.service.Splb_sService;
import com.byxm.wlgw.service.SpppService;
import com.byxm.wlgw.service.SpxhService;
import com.byxm.wlgw.service.SpxxService;
import com.byxm.wlgw.service.SpxxpxService;
/**
 * 商品信息管理
 * @author Administrator
 *
 */
public class Spxx_glAction  extends BaseAction{
	private static final long serialVersionUID = 1L;
	
	private String cpage="1";//当前页数
	
	private String splbfid="0";
	
	private List<Splb_fBean> listSplbf;//大类别
	
	private List<SpxxpxBean> lstSpxxpx;//商品信息排序
	
	private Map<String,Object> mapSplbs=null;//各种子类别
	
	private int iFlaglb=0;//
	
	private Pages page;
	
	private SpxxBean spxxBean;
	
	private List<SpxxBean> lstSpxx;//商品信息List;
	
	private List<JgqjBean> lstJgqj;
	
	private String pageNumSet="20";//每页显示的记录条数,默认20条

	/**
	 * 查看商品信息(分页)
	 * @return
	 */
	public String list(){
		SpxxpxService spxxpxService=new SpxxpxService();
		SpxxService spxxService=new SpxxService();
		JgqjService jgqjService=new JgqjService();
		Splb_fService splb_fService=new Splb_fService();
		lstJgqj=jgqjService.getJgqj();//价格区间
		listSplbf=splb_fService.findSplbf();//商品大类别
		lstSpxxpx=spxxpxService.findSpxxPx();
		
		if(this.panduanSpxxBean()){//如果为空的话就查找出各种类别
			spxxBean=new SpxxBean();
		}else{
			mapSplbs=new HashMap<String,Object>();
			Splb_sService splb_sService=new Splb_sService();
			SpppService spppService=new SpppService();
			SpxhService spxhService=new SpxhService();
			List<Splb_sBean> lstSplbs;
			List<SpppBean> lstSppp;
			List<SpxhBean> lstSpxh;
			try {
				lstSplbs=splb_sService.getSplb_s(spxxBean.getSplbfid());
				lstSppp=spppService.getSppp(spxxBean.getSplbfid());
				lstSpxh=spxhService.getSpxh(spxxBean.getSplbfid());
				mapSplbs.put("lstSplbs", lstSplbs);
				mapSplbs.put("lstSppp", lstSppp);
				mapSplbs.put("lstSpxh", lstSpxh);
				lstJgqj=jgqjService.getJgqj();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		if(page==null){
			page=new Pages();
		}
		//设置一下排序
		spxxBean.setSpxxpx(spxxpxService.findSpxxpxBean(spxxBean.getSpxxpx().getPxid()));
		page.setCpage(Integer.parseInt(cpage));
		page=this.getSpxxPages(spxxBean,Integer.parseInt(pageNumSet),page.getCpage()+"");
		
		try {
			//获得商品list
			lstSpxx=spxxService.findSpxxListgl(spxxBean, page);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		
		return "success_01";
	}
	/**
	 * 加载单个商品信息
	 * @return
	 */
	public String load(){
		Splb_fService splbfService=new Splb_fService();
		SpppService spppService=new SpppService();
		JgqjService jgqjService=new JgqjService();
		if(splbfid.trim()=="0"){
			listSplbf=splbfService.findSplbf();
		}
		if(splbfid.trim()!="0"){
			mapSplbs=new HashMap<String,Object>();
			Splb_sService splb_sService=new Splb_sService();
			try {
				listSplbf=splbfService.findSplbf();//商品大类别
				List<Splb_sBean> lstSplbs=splb_sService.getSplb_s(Integer.parseInt(splbfid));
				List<SpppBean> lstSppp=spppService.getSppp(Integer.parseInt(splbfid));
				List<JgqjBean> lstJgqj=jgqjService.getJgqj();
				mapSplbs.put("lstSplbs", lstSplbs);//商品功能
				mapSplbs.put("lstSppp", lstSppp);//商品品牌
				mapSplbs.put("lstJgqj", lstJgqj);//价格区间
				iFlaglb=1;
			} catch (NumberFormatException e) {
				e.printStackTrace();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		
		
		return "success_02";
	}
	
	/**
	 * 保存
	 */
	public String save(){
		SpxxService spxxService=new SpxxService();
		//商品展示图片集的str
		StringBuffer spzstpsStr=new StringBuffer();
		StringBuffer spmstpsStr=new StringBuffer();
		//拿出商品货号
		String sphh=spxxBean.getSphh();
		//根目录名
		String sptpUrl=spxxBean.getSptpUrl();//根目录名
		//组装展示图片URLS
		for(int i=1;i<=spxxBean.getSpzstpNum();i++){
			spzstpsStr.append(sptpUrl).append("/spzs/").append(sphh).append("-"+i).append(".jpg").append(",");
		}
		//组装秒函数图片URLS;
		for(int i=1;i<=spxxBean.getSpmstpNum();i++){
			spmstpsStr.append(sptpUrl).append("/spms/").append(sphh).append("-"+i).append(".jpg").append(",");
		}
		spxxBean.setSptp(sptpUrl+"/sptp/"+sphh+".jpg");
		spxxBean.setSpzstp(spzstpsStr.deleteCharAt(spzstpsStr.length()-1).toString());
		spxxBean.setSpms(spmstpsStr.deleteCharAt(spmstpsStr.length()-1).toString());
		spxxBean.setKcsl(0);//新增时候 默认保存库存数量为0;
		try {
			spxxService.saveSpxx(spxxBean);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "success_03";
	}
	
	
	
	/*********私有方法************/
	@SuppressWarnings("unused")
	private boolean panduanSpxxBean(){
		if(spxxBean==null||
		(spxxBean.getSplbfid()==0&&
		spxxBean.getSplbsid()==0)&&
		spxxBean.getPpid()==0&&
		spxxBean.getJgqjid()==0&&
		spxxBean.getKczt()==0&&
		spxxBean.getXhid()==0&&
		spxxBean.getSearchString().trim()==""){
		     return true;
		}else{
			return false;
		}
	}
	
	private SpxxBean getQtSpxxBean(){
		
		return null;
	}

	
	
	
	/********setter getter*************/

	public String getSplbfid() {
		return splbfid;
	}
	public void setSplbfid(String splbfid) {
		this.splbfid = splbfid;
	}


	public Pages getPage() {
		return page;
	}
	public void setPage(Pages page) {
		this.page = page;
	}
	public List<Splb_fBean> getListSplbf() {
		return listSplbf;
	}
	public void setListSplbf(List<Splb_fBean> listSplbf) {
		this.listSplbf = listSplbf;
	}
	public Map<String, Object> getMapSplbs() {
		return mapSplbs;
	}
	public void setMapSplbs(Map<String, Object> mapSplbs) {
		this.mapSplbs = mapSplbs;
	}
	public int getiFlaglb() {
		return iFlaglb;
	}
	public void setiFlaglb(int iFlaglb) {
		this.iFlaglb = iFlaglb;
	}
	public SpxxBean getSpxxBean() {
		return spxxBean;
	}
	public void setSpxxBean(SpxxBean spxxBean) {
		this.spxxBean = spxxBean;
	}
	public List<SpxxBean> getLstSpxx() {
		return lstSpxx;
	}
	public void setLstSpxx(List<SpxxBean> lstSpxx) {
		this.lstSpxx = lstSpxx;
	}
	public List<JgqjBean> getLstJgqj() {
		return lstJgqj;
	}
	public void setLstJgqj(List<JgqjBean> lstJgqj) {
		this.lstJgqj = lstJgqj;
	}
	public String getCpage() {
		return cpage;
	}
	public void setCpage(String cpage) {
		this.cpage = cpage;
	}
	public String getPageNumSet() {
		return pageNumSet;
	}
	public void setPageNumSet(String pageNumSet) {
		this.pageNumSet = pageNumSet;
	}
	public List<SpxxpxBean> getLstSpxxpx() {
		return lstSpxxpx;
	}
	public void setLstSpxxpx(List<SpxxpxBean> lstSpxxpx) {
		this.lstSpxxpx = lstSpxxpx;
	}






	
}
